package com.zxsc.zxmusic.db;

import android.content.Context;
import android.util.SparseArray;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.zxsc.zxmusic.manager.SongManager;
import com.zxsc.zxmusic.model.LastSong;
import com.zxsc.zxmusic.model.SongInfo;
import com.zxsc.zxmusic.model.SongMenuModel;
import com.zxsc.zxmusic.model.UserModel;

import java.util.List;

import u.aly.db;

public class SongDb {

	/**
	 * 保存歌曲列表到数据库
	 * @param ctx
	 * @param songInfos
	 */
	public static void saveSongInfos(Context ctx, List<SongInfo> songInfos) {
		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			dbUtils.configAllowTransaction(true);
			dbUtils.saveAll(songInfos);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存歌曲到数据库
	 * @param ctx
	 * @param song
	 */
	public static synchronized void saveSong(Context ctx, SongInfo song) {

		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			dbUtils.update(song);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 获取全部歌曲
	 * @param ctx
	 * @return
	 */
	public static SparseArray<SongInfo> getTotalSongInfo(Context ctx) {
		List<SongInfo> infoList = null;
		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			infoList = dbUtils.findAll(SongInfo.class);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		SparseArray<SongInfo> infos = new SparseArray<>();
		if (infoList == null) {
			return infos;
		}

		for (SongInfo info : infoList) {
			infos.put(info.getId(), info);
		}
		return infos;
	}

	/**
	 * 删除全部歌曲
	 * @param ctx
	 */
	public static void deleteAllSongInfo(Context ctx) {
		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			dbUtils.dropTable(SongInfo.class);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过歌曲ID删除歌曲
	 * @param ctx
	 * @param id
	 */
	public static synchronized void deleteSongById(Context ctx, int id) {
		SongInfo info = SongManager.with(ctx).getSongById(id);
		if (info == null)
			return;
		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			dbUtils.delete(info);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存最后一次播放歌曲
	 * @param ctx
	 * @param song
	 */
	public static synchronized void saveLastSong(Context ctx, SongInfo song) {
		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			dbUtils.dropTable(LastSong.class);
			LastSong lastSong = new LastSong();
			lastSong.setId(song.getId());
			dbUtils.save(lastSong);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取最后一次播放歌曲
	 * @param ctx
	 * @return
	 */
	public static synchronized LastSong getLastSong(Context ctx) {
		LastSong song = null;
		try {
			DbUtils dbUtils = DbUtils.create(ctx);
			song = dbUtils.findFirst(LastSong.class);
			dbUtils.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return song;
	}

	

	// 歌曲收藏
	/**
	 * 检测是否可以收藏歌曲
	 * 
	 * @param context
	 * @param user
	 * @param info
	 * @return
	 */
	public static synchronized boolean favoritSong(Context context,
			UserModel user, SongInfo info,SongMenuModel menu) {
		boolean flag = false;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			long count = dbUtils.count(Selector.from(SongMenuModel.class)
					.where("userId", "=", user.getUserId())
					.and("songId", "=", info.getId())
					.and("id", "=", menu.getId())
					);

			if (count <= 0) {
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 收藏歌曲
	 * @param context
	 * @param menuModel
	 * @return
	 */
	public static synchronized boolean favoritSongYes(Context context,SongMenuModel menuModel){
		boolean flag = false;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			dbUtils.save(menuModel);
				flag = true;
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	

	/**
	 * 登陆
	 * 
	 * @param context
	 * @param userName
	 * @param userPwd
	 * @param lister
	 */
	public static void login(Context context, String userName, String userPwd,
			OnLoginLister lister) {
		try {
			DbUtils dbUtils = DbUtils.create(context);
			UserModel user = dbUtils.findFirst(Selector.from(UserModel.class)
					.where("userName", "=", userName)
					.and("userPwd", "=", userPwd));

			if (user != null && user.getUserName() != null
					&& user.getUserName().length() > 0) {
				lister.onSuccess(user);
			} else {
				lister.onFail("登陆失败，用户名或密码错误！");
			}

		} catch (Exception e) {
			lister.onFail(e.getMessage());
		}
	}

	/**
	 * 注册
	 * 
	 * @param context
	 * @param user
	 * @param lister
	 */
	public static synchronized void register(Context context, UserModel user,
			OnRegisterLister lister) {
		try {
			DbUtils dbUtils = DbUtils.create(context);
			long count = dbUtils.count(Selector.from(UserModel.class)
					.where("userName", "=", user.getUserName())
					.and("userPwd", "=", user.getUserPwd()));

			if (count < 1) {
				dbUtils.save(user);
				lister.onSuccess();
			} else {
				lister.onFail("注册失败，用户已存在！");
			}

		} catch (Exception e) {
			lister.onFail(e.getMessage());
		}
	}

	public interface OnRegisterLister {
		public void onSuccess();

		public void onFail(String msg);
	}

	public interface OnLoginLister {
		public void onSuccess(UserModel userModel);

		public void onFail(String msg);
	}

	// 歌单

	/**
	 * 检查是否可以创建歌单
	 * 
	 * @param context
	 * @param user
	 * @param songMenuModel
	 * @return
	 */
	public static synchronized boolean isCreateMenu(Context context,
			UserModel user, SongMenuModel songMenuModel) {
		boolean flag = false;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			long count = dbUtils.count(Selector.from(SongMenuModel.class)
					.where("userId", "=", user.getUserId())
					.and("menuName", "=", songMenuModel.getMenuName())
					.and("songCount", "=", -1));

			if (count <= 0) {
				flag = true;
			} else {
				flag = false;
			}
		} catch (Exception exception) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 创建歌单
	 * 
	 * @param context
	 * @param user
	 * @param songMenuModel
	 * @return
	 */
	public static synchronized boolean createMenu(Context context,
			SongMenuModel songMenuModel) {
		boolean flag = false;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			dbUtils.save(songMenuModel);
			flag = true;
		} catch (Exception exception) {
			flag = false;
		}
		return flag;
	}
	
	
	/**
	 * 通过用户ID获取用户歌单
	 * 
	 * @param context
	 * @param userId
	 * @return
	 */
	public static synchronized List<SongMenuModel> getAllSongMenu(
			Context context, String userId) {
		List<SongMenuModel> list = null;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			list = dbUtils.findAll(Selector.from(SongMenuModel.class).where(
					"userId", "=", userId)
					.and("songCount", "=", -1));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
	
	
	/**
	 * 通过歌单查询歌曲
	 * @param context
	 * @param model
	 * @return
	 */
	public static synchronized List<SongMenuModel> findAllSongs(Context context, SongMenuModel model){
		List<SongMenuModel> list = null;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			list = dbUtils.findAll(Selector.from(SongMenuModel.class).where(
					"userId", "=", model.getUserId())
					.and("songCount", "!=", -1)
					.and("id", "=", model.getId())
					);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
	
	/**
	 * 删除歌单
	 * @param context
	 * @param menuModel
	 * @return
	 */
	public static synchronized boolean deleteSongMenu(Context context,SongMenuModel menuModel){
		boolean flag=false;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			WhereBuilder builder=WhereBuilder.b("menuName", "=", menuModel.getMenuName())
					.and("userId", "=", menuModel.getUserId());
			dbUtils.delete(SongMenuModel.class, builder);
			flag=true;
		} catch (Exception e) {
			flag=false;
		}
		return flag;
	}
	
	/**
	 * 编辑歌单
	 * @param context
	 * @param menuModel
	 * @return
	 */
	public static synchronized boolean editSongMenu(Context context,SongMenuModel menuModel,String newName){
		boolean flag=false;
		try {
			DbUtils dbUtils = DbUtils.create(context);
			long count=dbUtils.count(Selector.from(SongMenuModel.class)
					.where("userId", "=", menuModel.getUserId())
					.and("songCount", "=", -1)
					.and("menuName", "=", newName));
			
			if(count<=0){//可以修改
				WhereBuilder builder=WhereBuilder.b("userId", "=", menuModel.getUserId())
						.and("songCount", "=", -1)
						.and("menuName", "=", menuModel.getMenuName());
						menuModel.setMenuName(newName);
				dbUtils.update(menuModel,builder , "menuName");
				flag=true;
			}else{
				flag=false;	
			}
		} catch (Exception e) {
			flag=false;
		}
		return flag;
	}
}
